package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.HairstyleTagsDomain;
import com.xingkeduo.pojo.json.HairstyleTagsJson;
import com.xingkeduo.pojo.query.HairstyleTagsQuery;
import com.xingkeduo.pojo.table.HairstyleTagsTable;
import com.xingkeduo.util.tool.ReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 * 标签管理
 *
 * @author chenxiangda <chenxiangda@xingkeduo.com>
 * @version id:v0.1
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Slf4j
@RestController
@RequestMapping(CMS_URL_PREFIX +"/hairstyletags")
public class HairstyleTagsController extends InitController {

    @Autowired
    private HairstyleTagsDomain hairstyleTagsDomain;

    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            HairstyleTagsQuery hairstyleTagsQuery = HairstyleTagsJson.createQueryForHairstyleTags(urlParam);
            long id = hairstyleTagsQuery.getId();
            HairstyleTagsTable hairstyleTagsTable = hairstyleTagsDomain.getHairstyleTagsInfoById(id);
            return returnUtil.getSuccess(HairstyleTagsJson.toJson(hairstyleTagsTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody @Valid HairstyleTagsTable hairstyleTagsTable , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
           // this.errMsg(bindingResult);
            hairstyleTagsTable.setCreatedBy(this.getOperator(request));
            log.info("创建发型标签接口请求参数:{}", JSON.toJSONString(hairstyleTagsTable));
            int hairstyleTags = hairstyleTagsDomain.createHairstyleTags(hairstyleTagsTable);
            log.info("创建发型标签接口返回值:{}", JSON.toJSONString(hairstyleTags));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody @Valid HairstyleTagsTable hairstyleTagsVo , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            HairstyleTagsTable hairstyleTagsTable = HairstyleTagsJson.createTableForModifyHairstyleTags(urlParam,hairstyleTagsVo);
            hairstyleTagsTable.setUpdatedBy(this.getOperator(request));
            log.info("修改发型标签接口请求参数:{}", JSON.toJSONString(hairstyleTagsTable));
            int i = hairstyleTagsDomain.modifyHairstyleTags(hairstyleTagsTable);
            log.info("修改发型标签接口返回值:{}", JSON.toJSONString(i));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            HairstyleTagsQuery hairstyleTagsQuery = HairstyleTagsJson.createQueryForHairstyleTags(urlParam);
            long id = hairstyleTagsQuery.getId();
            hairstyleTagsDomain.delHairstyleTags(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    @RequestMapping(value = "/lists/{urlParam:.+}", method = RequestMethod.GET)
    public String doGetLists(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            HairstyleTagsQuery hairstyleTagsQuery = new HairstyleTagsQuery();

            hairstyleTagsQuery =  JSON.parseObject(urlParam,HairstyleTagsQuery.class);

            List<HairstyleTagsTable> hairstyleTagsList= hairstyleTagsDomain.getHairstyleTagslists(hairstyleTagsQuery);

            return returnUtil.getSuccess(HairstyleTagsJson.toJson(hairstyleTagsList));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }
}
